<?php
uses('sanitize');

class ImportdataController extends AppController{
	var $name = 'Importdata';
	var $uses = array('Page', 'Category');
	var $components = array('Simplepie', 'RequestHandler', 'Textual');
	var $helpers = array('Form');
	
	function admin_index(){
   		if($this->RequestHandler->isPost()){
			$count = 0;
			$items = $this->Simplepie->feed($this->data['Rss']['url']);
			$this->set('items', $items);
			foreach($items as $item){
				$cat = $item->get_category();
				$this->data['Category']['title'] = $cat->get_label();
				$this->data['Category']['slug'] = $this->Textual->remplace_char($this->data['Category']['title'], null, 20);
				
				$category = $this->Category->findBySlug($this->data['Category']['slug']);
				if(!empty($category)){
					$this->data['Page']['category_id'] = $category['Category']['id'];	
				}else{
					$this->Category = new Category();
					$this->Category->save($this->data);
					$this->data['Page']['category_id'] = $this->Category->id;
				}
				
				$this->Page = new Page();
				$san = new Sanitize();
				
				$this->data['Page']['title'] = $san->stripAll($item->get_title());
		
				$this->data['Page']['slug'] = $san->paranoid($this->Textual->remplace_char($this->data['Page']['title'], null, 20), array('-'));;
				$this->data['Page']['created'] = $item->get_date('Y-m-d H:i:s');
				
				$this->data['Page']['content'] = $item->get_content();
				
				//$this->data['Page']['online'] = 1;
				$this->data['Page']['user_id'] = $this->user['id'];
				
				$this->Page->save($this->data);
				$count += 1;
			}
			$this->Session->setFlash($count.' Datas were imported from feed.');
		}
	}
	
	
}

?>
